package chapter2.version1;

import chapter2.Apple;

import java.util.ArrayList;
import java.util.List;

public class AppleVersionOne {
    /**
     * 筛选所有的绿苹果
     * @param inventory
     * @return
     */
    public static List<Apple> filterGreenApples(List<Apple> inventory){
        List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if ("green".equals(apple.getColor())){
                result.add(apple);
            }
        }
        return result;
    }

    /**
     * 将颜色参数化
     * @param inventory
     * @param color
     * @return
     */
    public static List<Apple> filterAppleByColor(List<Apple> inventory, String color){
        List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if (color.equals(apple.getColor())){
                result.add(apple);
            }
        }
        return result;
    }

    /**
     * 筛选重量
     */
    public static List<Apple> filterApplesByteWeight(List<Apple> inventory,int weight){
        List<Apple> result = new ArrayList<>();
        for(Apple apple: inventory){
            if (apple.getWeight() > weight){
                result.add(apple);
            }
        }
        return result;
    }


    /**
     * 对每个属性进行筛选
     */
    public static List<Apple> filterApples(List<Apple> inventory, String color, int weight, boolean flag){
        List<Apple> result = new ArrayList<>();
        for (Apple apple: inventory){
            if ((flag&&apple.getColor().equals(color))||(!flag && apple.getWeight() > weight)){
                result.add(apple);
            }
        }
        return result;
    }
}
